package server.net.packets;

import java.io.File;
import java.net.URLDecoder;
import java.util.HashMap;

public class PacketHandlers {

	HashMap<Byte, PacketHandler> packetHandlers = new HashMap<Byte, PacketHandler>();

	public void loadPacketHandlers() throws Exception {
		File[] files = new File(URLDecoder.decode(PacketHandler.class.getResource("packethandlers").getFile(), "UTF-8")).listFiles();
		System.out.println("Loading packet handlers...");		
		for(File f : files) {
			PacketHandler p = (PacketHandler)Class.forName(PacketHandler.class.getPackage().getName() + ".packethandlers." + f.getName().replace(".class","")).newInstance();
			packetHandlers.put(p.getHandledId(), p);
			System.out.println("\t- " + f.getName().replace(".class",""));
		}
		System.out.println("Packet handlers loaded!");
	}

	public PacketHandler getPacketHandler(byte id) {
		return packetHandlers.get(id);
	}

}
